﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using HRM.DAL.DAO;

namespace HRM.DAL.Gateway
{
    public class CodeGateway:DBGateway
    {
        public Code GetLastEmployeeNumber()
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = @"select * from tblCode";
                SqlDataReader reader = CommandObj.ExecuteReader();
                Code code=new Code();
                while (reader.Read())
                {
                    code.Number = Convert.ToInt32(reader[1]);
                }
                return code;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                ConnectionObj.Close();
            }
        }

        public bool SaveCode(Code code)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("insert into tblCode values(@number,@codeName)");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@number", SqlDbType.Int).Value = code.Number;
                CommandObj.Parameters.Add("codeName", SqlDbType.VarChar).Value = code.CodeName;
                CommandObj.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                ConnectionObj.Close();
            }
        }

        public int GetEmpCodeId(string codeName)
        {
            try
            {
                int id = 0;
                ConnectionObj.Open();
                CommandObj.CommandText = @"select Id from tblCode where CodeName='"+codeName+"'";
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    id = Convert.ToInt32(reader[0]);
                }
                return id;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                ConnectionObj.Close();
            }
        }
    }
}